package com.example.imchat.model;

import android.content.Context;
import android.content.Intent;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.example.imchat.model.bean.GroupInfo;
import com.example.imchat.model.bean.InvivationInfo;
import com.example.imchat.model.bean.UserInfo;
import com.example.imchat.utils.Constant;
import com.example.imchat.utils.SpUtils;
import com.hyphenate.EMContactListener;
import com.hyphenate.EMGroupChangeListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMucSharedFile;

import java.util.List;

/*
* lee
* 全局监听事件
* 对方拒绝群邀请  不显示的问题
* */
public class EventListener {

    private Context mContext;
    private final LocalBroadcastManager mLBM;

    public EventListener(Context Context) {
        mContext = Context;

        //创建一个发送广播管理者
        mLBM = LocalBroadcastManager.getInstance(mContext);

        //注册一个联系人变化的监听
        EMClient.getInstance().contactManager().setContactListener(emContactListener);

        //注册一个群信息变化的监听

        EMClient.getInstance().groupManager().addGroupChangeListener(eMGroupChangeListener);

    }
        //联系人邀请的广播
    private final EMContactListener emContactListener= new EMContactListener() {
        @Override
        public void onContactAdded(String hxid) {
            //增加了联系人时回调此方法
            //数据更新
            Model.getInstance().getDbManager().getContactTableDao().saveContact(new UserInfo(hxid),true);
            //发送联系人广播

            mLBM.sendBroadcast(new Intent(Constant.CONTACT_CHANGED));
        }

        @Override
        public void onContactDeleted(String hxid) {
            //被删除时回调此方法
            //数据更新
            Model.getInstance().getDbManager().getContactTableDao().deleteContactByHxid(hxid);
            Model.getInstance().getDbManager().getInviteTableDao().removeInvitation(hxid);
            //发送联系人广播
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_CHANGED));
        }

        @Override
        public void onContactInvited(String hxid, String reason) {
            //收到添加好友邀请
            InvivationInfo invationInfo = new InvivationInfo();
            invationInfo.setUser(new UserInfo(hxid));
            invationInfo.setReason(reason);
            invationInfo.setStatus(InvivationInfo.InvitationStatus.NEW_INVITE);

            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invationInfo);
            //红点
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);

            //邀请信息变化的广播
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));

        }

        @Override
        public void onFriendRequestAccepted(String hxid) {

            //同意我的邀请
            //数据更新
            InvivationInfo invationInfo = new InvivationInfo();
            invationInfo.setUser(new UserInfo(hxid));
            invationInfo.setStatus(InvivationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER);

            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invationInfo);

            //红点
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);

            //同意邀请信息变化的广播
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));

        }

        @Override
        public void onFriendRequestDeclined(String hxid) {
            //拒绝我的邀请
            InvivationInfo invationInfo = new InvivationInfo();
            invationInfo.setUser(new UserInfo(hxid));
            invationInfo.setStatus(InvivationInfo.InvitationStatus.INVITE_DECLINED);
            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invationInfo);
            //红点
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);

            //拒绝邀请信息变化的广播
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));
        }
    };

        /*
        * 群组
        * */

        //群邀请的广播
    private final EMGroupChangeListener eMGroupChangeListener = new EMGroupChangeListener() {
            //当前用户收到加入群组邀请
        @Override
        public void onInvitationReceived(String groupId, String groupName, String inviter, String reason) {
            //数据更新
            InvivationInfo invitationInfo= new InvivationInfo();
            invitationInfo.setReason(reason);
            invitationInfo.setGroup(new GroupInfo(groupName,groupId,inviter));
            invitationInfo.setStatus(InvivationInfo.InvitationStatus.NEW_GROUP_INVITE);
            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
            //发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));

        }
            //用户申请加入群
        @Override
        public void onRequestToJoinReceived(String groupId, String groupName, String applicant, String reason) {

            //数据更新
            InvivationInfo invitationInfo = new InvivationInfo();
            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);
            invitationInfo.setReason(reason);
            invitationInfo.setGroup(new GroupInfo(groupName,groupId,applicant));
            invitationInfo.setStatus(InvivationInfo.InvitationStatus.GROUP_ACCEPT_APPLICATION);
            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
            //发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));

        }
            //加群申请被对方接受
        @Override
        public void onRequestToJoinAccepted(String groupId, String groupName, String accepter) {

            //数据更新
            InvivationInfo invitationInfo = new InvivationInfo();
            invitationInfo.setGroup(new GroupInfo(groupName,groupId,accepter));
            invitationInfo.setStatus(InvivationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED);
            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);
            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
            //发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }
            //收到群申请被拒绝
        @Override
        public void onRequestToJoinDeclined(String groupId, String groupName, String decliner, String reason) {
            //数据更新
            InvivationInfo invitationInfo = new InvivationInfo();
            invitationInfo.setReason(reason);
            invitationInfo.setGroup(new GroupInfo(groupName,groupId,decliner));
            invitationInfo.setStatus(InvivationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED);
            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
            //发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }

            //群组邀请被接受
        @Override
        public void onInvitationAccepted(String groupId, String inviter, String reason) {
            //数据更新
            InvivationInfo invitationInfo = new InvivationInfo();
            invitationInfo.setReason(reason);
            invitationInfo.setGroup(new GroupInfo(groupId,groupId,inviter));
            invitationInfo.setStatus(InvivationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED);
            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
            //发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }

            //群组邀请被拒绝
        @Override
        public void onInvitationDeclined(String groupId, String inviter, String reason) {

            //数据更新
            InvivationInfo invitationInfo = new InvivationInfo();
            invitationInfo.setReason(reason);
            invitationInfo.setGroup(new GroupInfo(groupId,groupId,inviter));
            invitationInfo.setStatus(InvivationInfo.InvitationStatus.GROUP_INVITE_DECLINED);
            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
            //发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }
            //当前登录用户被管理员移除出群组
        @Override
        public void onUserRemoved(String groupId, String groupName) {

        }
            //群解散
        @Override
        public void onGroupDestroyed(String groupId, String groupName) {

        }
        //自动同意加入群组
        @Override
        public void onAutoAcceptInvitationFromGroup(String groupId, String inviter, String reason) {

           /* //数据更新
            InvivationInfo invitationInfo= new InvivationInfo();
            invitationInfo.setReason(reason);
            invitationInfo.setGroup(new GroupInfo(groupId,groupId,inviter));
            invitationInfo.setStatus(InvivationInfo.InvitationStatus.NEW_GROUP_INVITE);
            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            //红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);
            //发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));*/
        }

        @Override
        public void onMuteListAdded(String s, List<String> list, long l) {

        }

        @Override
        public void onMuteListRemoved(String s, List<String> list) {

        }

        @Override
        public void onWhiteListAdded(String s, List<String> list) {

        }

        @Override
        public void onWhiteListRemoved(String s, List<String> list) {

        }

        @Override
        public void onAllMemberMuteStateChanged(String s, boolean b) {

        }

        @Override
        public void onAdminAdded(String s, String s1) {

        }

        @Override
        public void onAdminRemoved(String s, String s1) {

        }

        @Override
        public void onOwnerChanged(String s, String s1, String s2) {

        }

        @Override
        public void onMemberJoined(String s, String s1) {

        }

        @Override
        public void onMemberExited(String s, String s1) {

        }

        @Override
        public void onAnnouncementChanged(String s, String s1) {

        }

        @Override
        public void onSharedFileAdded(String s, EMMucSharedFile emMucSharedFile) {

        }

        @Override
        public void onSharedFileDeleted(String s, String s1) {

        }
    };
}
